package com.example.admin.controller.job;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.admin.entity.SysJob;
import com.example.admin.service.ISysJobService;
import com.example.quartz.ScheduleConstants;
import com.example.shrio.CurrentUser;
import com.example.util.APIState;
import com.example.util.AjaxResult;
import com.example.util.page.Pager;
import com.example.util.page.SeachParamsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.CronExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 定时任务调度表 前端控制器
 * </p>
 *
 * @author wangkai
 * @since 2020-09-29
 */
@RestController
@Api(value = "定时任务Controller", tags = {"定时任务操作接口"})
@RequestMapping("/sys/sysJob")
public class SysJobController {

    @Autowired
    private ISysJobService sysJobService;

    @ApiOperation(value = "定时任务列表", httpMethod = "POST")
    @PostMapping(value = "/page")
    public AjaxResult page(@RequestBody Pager pager) {
        Page<SysJob> page = new Page<>();
        page.setCurrent(pager.getPageNum());
        page.setSize(pager.getPageSize());
        QueryWrapper<SysJob> queryWrapper = new QueryWrapper<>();
        SeachParamsUtil.loadSearchParam(pager, queryWrapper);
        pager.convertPage(sysJobService.page(page, queryWrapper));
        return new AjaxResult(APIState.S00, pager);
    }

    @ApiOperation(value = "定时任务新增", httpMethod = "POST")
    @PostMapping
    public AjaxResult save(@RequestBody SysJob sysJob) {
        if (!CronExpression.isValidExpression(sysJob.getCronExpression())) {
            return new AjaxResult(APIState.S01, "cron表达式错误");
        }
        sysJob.setCreateBy(String.format("%s", CurrentUser.getAdminUser().getId()));
        sysJob.setCreateTime(new Date());
        sysJobService.saveSysJob(sysJob);
        return new AjaxResult(APIState.S00, "保存成功");
    }

    @ApiOperation(value = "定时任务修改", httpMethod = "PUT")
    @PutMapping
    public AjaxResult update(@RequestBody SysJob sysJob) {
        if (!CronExpression.isValidExpression(sysJob.getCronExpression())) {
            return new AjaxResult(APIState.S01, "cron表达式错误");
        }
        sysJob.setUpdateBy(String.format("%s", CurrentUser.getAdminUser().getId()));
        sysJob.setUpdateTime(new Date());
        sysJobService.updateSysJob(sysJob);
        return new AjaxResult(APIState.S00, "保存成功");
    }

    @ApiOperation(value = "定时任务删除", httpMethod = "DELETE")
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable Integer[] ids) {
        sysJobService.deleteSysJob(ids);
        return new AjaxResult(APIState.S00, "保存成功");
    }

    @ApiOperation(value = "定时任务启用/停用", httpMethod = "PUT")
    @PutMapping("start")
    public AjaxResult start(Integer jobId) {
        SysJob sysJob = sysJobService.getById(jobId);
        if (sysJob == null) {
            return new AjaxResult(APIState.S01, "当前任务不存在");
        }
        if (sysJob.getStatus().equals(ScheduleConstants.Status.NORMAL.getValue())) {
            sysJob.setStatus(ScheduleConstants.Status.PAUSE.getValue());
        } else {
            sysJob.setStatus(ScheduleConstants.Status.NORMAL.getValue());
        }
        sysJob.setUpdateTime(new Date());
        sysJob.setUpdateBy(String.format("%s",CurrentUser.getAdminUser().getId()));
        if (sysJobService.start(sysJob)) {
            return new AjaxResult(APIState.S00, "更改成功");
        }
        return new AjaxResult(APIState.S01, "更改失败");
    }

    @ApiOperation(value = "执行一次", httpMethod = "GET")
    @GetMapping("/run/{id}")
    public AjaxResult run(@PathVariable Integer id) {
        SysJob sysJob = sysJobService.getById(id);
        if (sysJob == null) {
            return new AjaxResult(APIState.S01, "当前任务不存在");
        }
        sysJobService.run(sysJob);
        return new AjaxResult(APIState.S00, "执行成功");
    }


}
